package com.li.controller;

import com.li.pojo.*;
import com.li.service.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

@Controller
public class ProjectController {

    @Resource
    ProjectService service;
    @Resource
    Course1Service service1;
    @Resource
    Course2Service service2;
    @Resource
    AllsortsService service3;
    @Resource
    RequireService service4;

    @RequestMapping("/projects")
    public String list(Model model){
        Collection<Project> projects=service.allProject( );
        model.addAttribute("projects",projects);
        return "project/list";
    }
    @GetMapping(value = "/toproject")
    public String toproject(Model model){

        return "project/add";
    }
    int i=1;
    @RequestMapping(value = "/project/add")
    //@ResponseBody
    public String addProject(MultipartFile file, HttpServletRequest request, HttpServletResponse response,Project project){
        String realPath = request.getServletContext().getRealPath("");
        String s=request.getParameter("pno");
        System.out.println(realPath);
        String filePath = "D:\\IDEA2\\jk\\shixun-623\\src\\main\\resources\\static\\img\\"+s+".jpg";
        i++;
        File dest = new File(filePath);
        try {
            file.transferTo(dest);
        } catch (IOException e) {
        }
        System.out.println("1");
        if(service.addProject(project))return "redirect:/projects";
        return "文件重复，添加失败";
    }
    @GetMapping("/project/{ptitle}")
    public String toupdateProject(
            @PathVariable("ptitle")String ptitle, Model model){

        Project project=new Project(ptitle);
        project=service.queryProject(project);
        model.addAttribute("pro",project);
        return "project/update";
    }

    @GetMapping("/detailproject/{ptitle}")
    public String detailsProject(
            @PathVariable("ptitle")String ptitle, Model model){
        Project project=new Project(ptitle);
        project=service.queryProject(project);
        model.addAttribute("pro",project);
        Collection<Course1> course1s=service1.allCourse1();
        model.addAttribute("course1s",course1s);
        Collection<Course2> course2s=service2.allCourse2();
        model.addAttribute("course2s",course2s);
        Collection<Allsorts> allsortss=service3.list();
        model.addAttribute("allsortss",allsortss);
        Collection<Require> requires=service4.allRequire();
        model.addAttribute("requires",requires);
        return "project/detail";
    }
    @RequestMapping(value = "/deleteproject/{ptitle}")
    public String deleteProject(@PathVariable("ptitle")String ptitle){
        Project project=new Project(ptitle);
        service.deleteProject(project);
        return "redirect:/projects";
    }

    @RequestMapping("/file")
    public String up(){
        return "project/upload";
    }
    @RequestMapping("/files")
    public String up1(){
        return "project/show";
    }

    @RequestMapping("/upload")
    public String upload(MultipartFile file, HttpServletRequest request, HttpServletResponse response) {
        //String s=request.getParameter("id");
        List<MultipartFile> files=((MultipartHttpServletRequest)request).getFiles("filename");
        //String realPath = request.getServletContext().getRealPath("");
        //System.out.println(realPath);
        //选择你的本地项目路径下文件存储路径，和前面application中对应
        int i=1;
        System.out.println("来了");
        for(MultipartFile f:files){
            System.out.println("我进来了");
            String fileName ="D:\\IDEA2\\jk\\shixun-623\\src\\main\\resources\\static\\img1\\"+i+".jpg";
            System.out.println(fileName);
            i++;
            File dest=new File(fileName);
            try {
                file.transferTo(dest);
            } catch (IOException e) {
            }

        }
        /*String filePath = "D:\\IDEA2\\jk\\shixun-623\\src\\main\\resources\\static\\img1\\"+1+".jpg";
        File dest = new File(filePath);
       try {
            file.transferTo(dest);
            *//*response.sendRedirect("project/show");*//*
        } catch (IOException e) {
        }*/

       return "project/show";
    }

}
